<?php


namespace app\api\service;


use app\common\model\BaseModel;
use app\api\model\Course as CourseModel;
use app\api\model\CourseOrder as CourseOrderModel;
use app\api\model\RemindCourse as RemindCourseModel;
use app\api\model\Teacher as TeacherModel;
use app\api\model\TeacherMoneyLog as TeacherMoneyLogModel;
use think\Db;

class CourseRemindService
{
    public static function getCourses()
    {
        $date = date('Y-m-d', time());
        return CourseModel::where('status', 3)->where('date', $date)->select();
    }

    /**
     * @param $time 设置时间
     * @param $type 1-开课提醒，2-完成提醒
     * @param $index 0-开始时间，1-完成时间
     * @return bool
     */
    public static function remind($remindTime, $type, $index = 0)
    {
        $courses = self::getCourses();
        foreach ($courses as $val) {
            $times = explode('-', $val['time']);
            if (!isset($times[$index])){
                return false;
            }
            $time = strtotime($val['date']. ' '. $times[$index]);
            if (time() + $remindTime >= $time) {
                Db::startTrans();
                try {
                    //完成课程
                    if ($index) {
                        $val->status = 4;
                        $val->save();
                        //是否全部完成
                        $courseCount = CourseModel::getCourseOrderCount($val->course_order_id, 4);
                        $courseOrder = CourseOrderModel::find($val->course_order_id);
                        if ($courseOrder && $courseCount == $courseOrder->num) {
                            //更新订单完成
                            $courseOrder->status = 3;
                            $courseOrder->save();
                            //教师
                            $teacher = TeacherModel::find($courseOrder->teacher_id);
                            if ($teacher) {
                                //记录教师账号明细
                                TeacherMoneyLogModel::add($teacher, $courseOrder->total_price, 1, $courseOrder->course_order_id);
                                //教师获得课程费
                                $teacher->setInc('money', $courseOrder->total_price);
                            }
                        }
                    }
                    self::courseRemind($val->course_id, $type, $index);
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                }
            }
        }
        return true;
    }

    /**
     * 课程提醒
     * @param $course
     * @param $time
     * @param $type
     * @return array
     */
    public static function courseRemind($courseId, $type, $timeIndex = false)
    {
        $data = self::dataFormat($courseId, $type, $timeIndex);
        $coursRemind = RemindCourseModel::getByCourseId($courseId, $type);
        if (!$coursRemind) {
            RemindCourseModel::create($data);
        }
        return true;
    }


    private static function dataFormat($courseId, $type, $timeIndex)
    {
        $course = CourseModel::with(['courseOrder', 'teacherCourse'])->find($courseId);
        if (!$course) {
            BaseModel::errorMsg(14000);
        }
        $times = explode('-', $course->time);
        return [
            'course_id' => $course->course_id,
            'teacher_course_id' => $course->teacher_course_id,
            'course_order_id' => $course->course_order_id,
            'user_id' => $course->course_order ? $course->course_order->user_id : 0,
            'teacher_id' => $course->teacher_course ? $course->teacher_course->teacher_id : 0,
            'time' => $timeIndex ? ($times[1]?? 0) : $times[0],
            'type' => $type
        ];
    }
}